package com.getword.util;

import org.apache.log4j.Logger;

import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

/**
 * Created by IntelliJ IDEA.
 * User: lwping
 * Date: 2010-8-25
 * Time: 9:26:36
 * To change this template use File | Settings | File Templates.
 */
public class ReflectUtil {
    private static final Logger logger = Logger.getLogger(ReportTemplate.class);

    public static Object getFieldValue(String fieldName, Object obj) {
        Method method = null;
        Object returnObj = null;
        String methodStr = "get";
        if (fieldName != null && fieldName.length() > 0) {
            String a = fieldName.substring(0, 1);
            methodStr += a.toUpperCase();
            methodStr += fieldName.substring(1);
        }
        try {
            method = obj.getClass().getMethod(methodStr);
            returnObj = method.invoke(obj);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return returnObj;
    }
}
